<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
    <title>社交平台</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<jsp:include page="/views/header.jsp"/>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-3">
            <div class="card">
                <div class="card-header">
                    <h5>日志分类</h5>
                </div>
                <div class="card-body">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link ${empty param.type ? 'active' : ''}"
                               href="${pageContext.request.contextPath}/post/list">全部日志</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link ${param.type eq 'public' ? 'active' : ''}"
                               href="${pageContext.request.contextPath}/post/list?type=public">公开日志</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link ${param.type eq 'friends' ? 'active' : ''}"
                               href="${pageContext.request.contextPath}/post/list?type=friends">好友日志</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link ${param.type eq 'mine' ? 'active' : ''}"
                               href="${pageContext.request.contextPath}/post/list?type=mine">我的日志</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <div class="card mb-3">
                <div class="card-body">
                    <a href="${pageContext.request.contextPath}/post/create" class="btn btn-primary">发布新日志</a>
                </div>
            </div>

            <c:if test="${empty posts}">
                <div class="card">
                    <div class="card-body">
                        <div class="alert alert-info">暂无日志</div>
                    </div>
                </div>
            </c:if>

            <c:forEach items="${posts}" var="post">
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="post-header d-flex align-items-center mb-3">
                            <img src="${post.user.avatar != null ? post.user.avatar : 'images/default-avatar.png'}"
                                 class="rounded-circle avatar-sm me-3" alt="作者头像">
                            <div>
                                <h5 class="mb-0">${post.user.nickname}</h5>
                                <small class="text-muted">${post.createdAt}</small>
                                <c:if test="${not empty post.location}">
                                    <small class="text-muted"> · ${post.location}</small>
                                </c:if>
                                <span class="badge bg-secondary ms-2">
                                    <c:choose>
                                        <c:when test="${post.visibility eq 'public'}">公开</c:when>
                                        <c:when test="${post.visibility eq 'friends'}">好友可见</c:when>
                                        <c:when test="${post.visibility eq 'private'}">仅自己可见</c:when>
                                    </c:choose>
                                </span>
                            </div>
                        </div>
                        <div class="post-content mb-3">
                            <p>${post.content}</p>
                        </div>
                        <div class="post-actions d-flex justify-content-between align-items-center">
                            <div>
                                <a href="${pageContext.request.contextPath}/post/detail?id=${post.postId}"
                                   class="btn btn-sm btn-outline-secondary me-2">查看详情</a>
                                <c:if test="${post.userId == currentUser.userId}">
                                    <a href="${pageContext.request.contextPath}/post/edit?id=${post.postId}"
                                       class="btn btn-sm btn-outline-secondary me-2">编辑</a>
                                    <form action="${pageContext.request.contextPath}/post/delete" method="post" class="d-inline">
                                        <input type="hidden" name="postId" value="${post.postId}">
                                        <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
                                    </form>
                                </c:if>
                            </div>
                            <div>
                                <span class="me-3">
                                    <i class="bi bi-hand-thumbs-up"></i> ${post.likeCount}
                                </span>
                                <span>
                                    <i class="bi bi-chat"></i> ${post.commentCount}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </c:forEach>

            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    <c:if test="${currentPage > 1}">
                        <li class="page-item">
                            <a class="page-link" href="?page=${currentPage - 1}&type=${param.type}" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    </c:if>

                    <c:forEach begin="1" end="${totalPages}" var="i">
                        <li class="page-item ${i == currentPage ? 'active' : ''}">
                            <a class="page-link" href="?page=${i}&type=${param.type}">${i}</a>
                        </li>
                    </c:forEach>

                    <c:if test="${currentPage < totalPages}">
                        <li class="page-item">
                            <a class="page-link" href="?page=${currentPage + 1}&type=${param.type}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </c:if>
                </ul>
            </nav>
        </div>
    </div>
</div>

<jsp:include page="/views/footer.jsp"/>